package com.dhecp.project.statistics.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.statistics.domain.ClientGathering;
import com.dhecp.project.statistics.service.ClientGatheringService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 客户应收统计 控制层
 * @author DH-Xxj
 * @version 2020年3月30日
 */

@Controller
@RequestMapping("/statistics/clientgathering")
public class ClientGatheringController extends BaseModuleController {
    private String moduleTable = ModuleTable.CLIENT_GATHERING;

    @Autowired
    private ClientGatheringService clientGatheringService;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
    
    /**
     * 客户应收统计列表
     * @param 
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("statistics:clientgathering:view")
    @GetMapping()
    public ModelAndView listPage(@RequestParam Map<String, Object> params){
		params.put("moduleTable", moduleTable);
    	params.put("entity", new ClientGathering()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("prefix","statistics/statistics"); //页面路径
    	
    	ModelAndView mv = this.openListPage(params);
    	mv.addObject("statistics", "clientgathering");
    	mv.addObject("statisticsName", "客户应收统计");
        return mv;
    }

    /**
     * 客户应收统计数据
     * @param params
     * @return TableDataInfo
     * @author DH-Xxj
     */
    @RequiresPermissions("statistics:clientgathering:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
        List<ClientGathering> list = new ArrayList<ClientGathering>();
        Integer countRow = 0;
        try {
            list = clientGatheringService.queryClientGatheringList(params);
            if (list != null && list.size() > 0){
            	countRow = list.get(0).getCountRow();
            }
        } catch (Exception e) {
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list, countRow);
    }
	
    /**
     * 数据导出Excel
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("statistics:clientgathering:export")
    @Log(title = "客户应收统计导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new ClientGathering()); 
        List<ClientGathering> list = clientGatheringService.queryClientGatheringList(params);
        
    	params.put("getFieldAttributesType", "export");
    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<ClientGathering> util = new ExportExcelUtils<ClientGathering>(moduleTable,new ClientGathering());
        return util.exportExcel(list, fieldList, "客户应收统计数据", "export");
    }
    
}
